package com.wbs.cluster.redis.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * TODO Swagger2配置
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

//    @Value("${swagger.enable}")
//    private boolean enableSwagger;
//
//    @Value("${swagger.contact}")
//    private String contact;

    @Bean
    public Docket createRestApi() {

        Parameter userToken = new ParameterBuilder()
                .name("userToken")
                .description("令牌token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        Parameter userId = new ParameterBuilder()
                .name("userId")
                .description("用户id")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();


//        if (enableSwagger) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wbs"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(Lists.newArrayList(userId, userToken));
//        } else {
//            return new Docket(DocumentationType.SWAGGER_2)
//                    .apiInfo(apiInfo())
//                    .select()
//                    .paths(PathSelectors.none())//开关关闭
//                    .build();
//        }


    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API说明文档")
                .description("restful 风格接口")
                .version("1.0")
                .build();
    }
}
